package main;

import java.sql.*;
import java.util.Scanner;


/**
 * Created by freewheel on 2016/9/30.
 */
public class Entry {

    private static Scanner sc;

    private static void initScanner() {
        sc = new Scanner(System.in);
    }

    public static Scanner getScanner() {
        return sc;
    }

    public static void main(String[] args) {
        System.out.println();
        initScanner();
        printWelcomeWord();
        enterMainMenu();
    }

    public static void enterMainMenu() {
        printMainMenu();
        Role role = getRoleFromUser();
        showMenuByRole(role);
    }

    private static void printWelcomeWord() {
        System.out.println("Welcome to sales System!\n");
    }

    private static void printMainMenu() {
        String mainMenu = "-----Main menu-----\n" +
                "What kinds of operation would you like to perform?\n" +
                "1. Operations for administrator\n" +
                "2. Operations for salesperson\n" +
                "3. Operations for manager\n" +
                "4. Exit this program\n" +
                "Enter your Choice: ";
        System.out.println(mainMenu);
    }

    public static void showMenuByRole(Role role) {
        switch (role) {
            case ADMINISTRATOR: {
                AdministratorHelper.printAdminOperationsMenu();
                int operation = getOptionNumberFromUser();
                AdministratorHelper.performOperationsByOption(operation);
                break;
            }
            case SALESPERSON: {
                SalespersonHelper.printSalespersonOperationsMenu();
                int operation = getOptionNumberFromUser();
                SalespersonHelper.performOperationsByOption(operation);
                break;
            }
            case MANAGER: {
                ManagerHelper.printManagerOperationsMenu();
                int operation = getOptionNumberFromUser();
                ManagerHelper.performOperationsByOption(operation);
                break;
            }
            case EXIT:
                exitProgram();
                break;
            case INVALID:
                System.out.println("Please input number 1 to 4 !");
                enterMainMenu();
        }
    }

    private static int getOptionNumberFromUser() {
        int option = -1;
        if (sc.hasNextInt()) {
            option = sc.nextInt();
        }
        sc.nextLine();
        return option;
    }

    private static Role getRoleFromUser() {
        int option = getOptionNumberFromUser();

        switch (option) {
            case 1:
                return Role.ADMINISTRATOR;
            case 2:
                return Role.SALESPERSON;
            case 3:
                return Role.MANAGER;
            case 4:
                return Role.EXIT;
        }
        return Role.INVALID;
    }


    private static void exitProgram() {
        Connection conn = DBHelper.getConnection();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Thanks for using Sales System! Bye!");
    }


}
